<template>
    <div class='comment-box'>
        <h3>发表评论</h3>
        <textarea placeholder="请输入要评论的内容（最多输入120字）" maxlength="120" v-model="msg"></textarea>
        <mt-button type='primary' size='large' class='handin' @click="postmoment">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments" :key='item.add_time'>
                <div class="cmt-title">
                    第{{ i+1 }}楼&nbsp;&nbsp;&nbsp;&nbsp;用户:{{ item.user_name }}&nbsp;&nbsp;&nbsp;&nbsp;发表时间:{{ item.add_time }}
                </div>
                <div class="cmt-body">
                   {{ item.content }}
                </div>
            </div>
        </div>
        <mt-button type='danger' size='large' plain @click="getmore">加载更多</mt-button>
    </div>
</template>

<script>
import {  Toast } from "mint-ui"
export default {
    data(){
        return{
            pageIndex: 1,
            comments:[],
            msg:''
        }
    },
    created(){
        this.initcomments()
        this.getComments();
    },
    methods:{
        initcomments(){   
            this.comments = JSON.parse(localStorage.getItem('comment'))||[];
        },
        getComments(){
            // localStorage.clear('comment');
            // if(!localStorage.getItem('comment')){
            //     localStorage.setItem('comment','[]')
            // }else{
            //     this.comments = localStorage.getItem('comment');
            // }
            try {
                this.$http.get('https://www.easy-mock.com/mock/5cf86de19b40a0058b942993/vue-001/comment/pageindex=' + this.pageIndex).then(result =>{
                    if(result.status === 200){
                        this.comments = this.comments.concat(result.body.data);
                    } 
                }) 
            } catch (error) {
                Toast('评论获取失败')
            }
        }, 
        getmore(){
            this.pageIndex++;
            this.getComments();
            console.log(this.pageIndex)
        },
        postmoment(){
            if(this.msg.trim().length ===0){
                return Toast("评论内容不能为空")
            }
        //     this.$http.post(""+this.$route.params.id,{
        //         content: this.msg.trim()
        //     }).then(result =>{
        //         if(result.body.status === 0){
        //             Toast('评论成功')

        //             var cmt = {
        //                 user_name : '李林同学',
        //                 add_time : Data.now(),
        //                 content : this.msg.trim()
        //             }

        //             this.comments.unshift(cmt);
        //             this.msg = '';

        //           }else{
        //             Toast('评论失败')
        //         }
        //     }
            var cmt = {
                    add_time : Date.now(),
                    user_name : "李林同学",
                    content : this.msg.trim()  
                }
            this.comments.unshift(cmt);
            let handledata = JSON.stringify(this.comments)
            localStorage.setItem('comment',handledata)
            this.msg = '';
            }
    },
    props:["id"]
}

</script>

<style lang="scss" scoped>
.comment-box{
    h3{font-size: 16px;}
    textarea{   
        font-size: 15px;
        margin-bottom:0px; 
    }

    .mint-button{
        font-size: 14px;
        height: 32px;
    }
    .handin{
        background-color: #0845ae;
    }


    .cmt-list{
        margin-top: 10px;
        .cmt-item{
            font-size:13px;
            .cmt-title{
                border-radius: 3px;
                background-color:#ccc;
                line-height: 30px;
            }

            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
    }




}

</style>



